package com.juzipi.thread.util;

import com.alibaba.fastjson.JSONObject;

import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author juzipi
 * @Date 2021/6/15 17:20
 * @Info
 */
public class MyTest {

    final static SimpleDateFormat simp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2000, 5000, 2, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(5000));
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10000; i++) {
            ToolMan toolMan = new ToolMan(countDownLatch);
            threadPoolExecutor.execute(toolMan);
        }
        countDownLatch.countDown();
    }

    static class ToolMan implements Runnable{
        CountDownLatch latch;

        public ToolMan(CountDownLatch latch){
            this.latch = latch;
        }

        @Override
        public void run() {
            long startTime = 0;
            try {
                startTime = System.currentTimeMillis();
                latch.await();
                System.out.println("开始请求");
                JSONObject jsonObject = httpPost("http://localhost:8060/get", null);
                System.out.println(jsonObject);
                System.out.println(jsonObject.get("code"));
                System.out.println(jsonObject.get("message"));
                System.out.println(jsonObject.get("content"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long endTime = System.currentTimeMillis();
            long lo = endTime-startTime;
            System.out.println("用时"+lo);
        }

        static JSONObject httpPost(String url, String param){
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectionRequestTimeout(2000).build();
            httpPost.setConfig(requestConfig);
            JSONObject jsonObject = null;
            if (param != null){
                StringEntity stringEntity = new StringEntity(param, "utf-8");
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/x-www-form-urlencoded");
                httpPost.setEntity(stringEntity);
            }
            try {
                CloseableHttpResponse execute = httpClient.execute(httpPost);
                if (execute.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                    String string = EntityUtils.toString(execute.getEntity(), "utf-8");
                    jsonObject = JSONObject.parseObject(string);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                httpPost.releaseConnection();
            }
            return jsonObject;
        }
    }

}
